using UnityEngine;

/// <summary>
/// 太阳池，使用抽象层重构
/// </summary>
public class SunPool : MonoBehaviour
{
    public static SunPool Instance { get; private set; }

    [SerializeField] private GameObject sunPrefab;
    [SerializeField] private int initialPoolSize = 10;
    [SerializeField] private int maxPoolSize = 100;

    private IPool<GameObject> m_Pool;

    private void Awake()
    {
        Instance = this;
        // 延迟初始化，确保 PoolManager 已经准备好
        StartCoroutine(DelayedInitialize());
    }

    private System.Collections.IEnumerator DelayedInitialize()
    {
        // 等待一帧，确保所有 Awake 方法都已执行
        yield return null;
        InitializePool();
    }

    private void InitializePool()
    {
        // ✅ 使用辅助类获取或创建 PoolManager
        var poolManager = PoolManagerHelper.GetOrCreatePoolManager();
        if (poolManager == null)
        {
            Logger.LogError("SunPool", "无法获取或创建 PoolManager！");
            return;
        }

        // ✅ 检查太阳预制体是否已分配
        if (sunPrefab == null)
        {
            Logger.LogError("SunPool", "SunPrefab is not assigned in SunPool!");
            return;
        }

        // ✅ 使用抽象层创建对象池
        m_Pool = poolManager.CreateGameObjectPool(
            sunPrefab, 
            initialPoolSize, 
            maxPoolSize, 
            transform
        );
        
        Logger.Log("SunPool", "SunPool initialized with abstract layer");
    }

    /// <summary>
    /// 获取太阳对象
    /// </summary>
    public GameObject GetSun()
    {
        return m_Pool?.Get();
    }

    /// <summary>
    /// 归还太阳对象
    /// </summary>
    public void ReturnSun(GameObject sun)
    {
        m_Pool?.Return(sun);
    }

    /// <summary>
    /// 获取池状态信息
    /// </summary>
    public void LogPoolStatus()
    {
        if (m_Pool != null)
        {
            Logger.Log("SunPool", $"Status - Total: {m_Pool.Count}, Active: {m_Pool.ActiveCount}");
        }
    }

    private void OnDestroy()
    {
        m_Pool?.Clear();
    }
}